<template>
    <div class="sign">
        <el-row>
            <el-col :span="20" :offset="2">
                <div class="sign-body">
                    <h2 style="text-align: left;">欢迎注册</h2>
                    <div style="text-align: left;">已有账户？<el-button type="text" @click="openLoginDialog">登录</el-button></div>
                    <el-form ref="userform" :model="userForm" :rules="rules" label-width="80px" size="small">
                        <el-form-item label="用户姓名" prop="name">
                            <el-input v-model="userForm.name"></el-input>
                        </el-form-item>
                        <el-form-item label="用户性别" prop="gender" style="text-align: left;">
                            <el-radio-group v-model="userForm.gender">
                                <el-radio label="男"></el-radio>
                                <el-radio label="女"></el-radio>
                            </el-radio-group>
                        </el-form-item>
                        <el-form-item label="上传头像" prop="img" style="text-align: left;">
                            <el-upload
                                class="avatar-uploader"
                                :limit="1"
                                action="http://120.26.2.5:15400/upload/file"
                                :show-file-list="false"
                                :on-success="handleAvatarSuccess"
                                :before-upload="beforeAvatarUpload">
                                <img v-if="userForm.img.length > 0" :src="imgUrl + userForm.img" class="avatar">
                                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                            </el-upload>
                        </el-form-item>
                        <el-form-item label="电话号码" prop="phone">
                            <el-input v-model="userForm.phone"></el-input>
                        </el-form-item>
                        <el-form-item label="出生日期" prop="birthday">
                            <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd" v-model="userForm.birthday" style="width: 100%;"></el-date-picker>
                        </el-form-item>
                        <el-form-item label="账户密码" prop="password">
                            <el-input type="password" v-model="userForm.password"></el-input>
                        </el-form-item>
                        <el-form-item label="确认密码" prop="checkPassword">
                            <el-input type="password" v-model="userForm.checkPassword"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button style="width: 100%;background-color: #BDCEFC;color: white;border-radius: 20px;border: none;" type="primary" @click="signUser">注册</el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </el-col>
        </el-row>
        <Login :dialog-visible="loginShow" @closeLoginDialog="closeLoginDialog"></Login>
    </div>
</template>

<script>
import {signUser} from '../api/api'
import Login from '@/components/Login.vue';
export default {
    name: 'sign',
    components:{
        Login
    },
    data(){
        var validatePass = (rule, value, callback) => {
            if (value === '') {
            callback(new Error('请再次输入密码'));
            } else if (value !== this.userForm.password) {
            callback(new Error('两次输入密码不一致!'));
            } else {
            callback();
            }
        };
        return {
            loginShow: false,
            imgUrl: '',
            userForm: {
                birthday: '',
                gender: '男',
                img: '',
                name: '',
                password: '',
                checkPassword: '',
                phone: '',
                type: 'user'
            },
            rules: {
                name: [
                    { required: true, message: '请输入用户姓名', trigger: 'blur' }
                ],
                phone: [
                    { required: true, message: '请输出电话号码', trigger: 'blur' }
                ],
                birthday: [
                    { required: true, message: '请输入出生日期', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入账户密码', trigger: 'blur' }
                ],
                checkPassword: [
                    { validator: validatePass, trigger: 'blur' }
                ]
            }
        }
    },
    mounted(){
        const that = this
        that.imgUrl = that.$store.state.imgUrl
    },
    methods: {
        openLoginDialog:function(){
            const that = this
            that.loginShow = true
        },
        closeLoginDialog:function(){
            const that = this
            that.loginShow = false
        },
        signUser:function(){
            const that = this
            this.$refs['userform'].validate((valid) => {
                if (valid) {
                    if(that.userForm.img.length <= 0){
                        that.$message({
                            message: '请上传用户头像',
                            type: 'error'
                        })
                        return
                    }
                    signUser(that.userForm).then(res => {
                        this.$message({
                            message: res.data
                        })
                        if(res.status === 200){
                            // this.loginShow = true
                        }
                    })
                } else {
                    return false;
                }
            });
        },
        handleAvatarSuccess(res, file) {
            const that = this
            that.userForm.img = res
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
            this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
            this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        }
    }
}
</script>

<style  scoped>
.el-row {
    height: 100%;
}
.el-col {
    height: 100%;
}
.sign-body{
    background-color: #F5F1F4;
    border-radius: 20px;
    margin-top: 10px;
    width: 100%;
    height: 600px;
    padding: 20px;
}
.avatar-uploader .el-upload {
    border: 1px dotted #525151;
    border-radius: 6px;
}
.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
}
.avatar {
    width: 100px;
    height: 100px;
    display: block;
}
</style>